package com.mmd.manage.web;

import com.mmd.bean.CODE;
import com.mmd.bean.WebJsonBean;
import com.mmd.common.entity.MmdUser;
import com.mmd.exception.GameException;
import com.mmd.manage.service.UserService;
import com.mmd.manage.vo.UserEo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping(value = "/api/login")
@Controller
public class LoginController {
	
	@Autowired
	private UserService userSerivce;

	@RequestMapping(value = "/login")
	@ResponseBody
	public WebJsonBean login(String account, String pwd, HttpServletResponse response){
		if (StringUtils.isEmpty(account)){
			throw new GameException(new CODE(9999, "请输入账号"));
		}
		if (StringUtils.isEmpty(pwd)){
			throw new GameException(new CODE(9999, "请输入密码"));
		}
		return new WebJsonBean(CODE.SUCCESS, userSerivce.login(account, pwd, response));
	}


	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping ("/logoff")
	@ResponseBody
	public WebJsonBean logoff(HttpServletRequest request, HttpServletResponse response, String adminToken){
		boolean result = userSerivce.logoff(request,response,adminToken);
		return new WebJsonBean(CODE.SUCCESS, result);
	}

	/**
	 * 用户注册
	 * @return
	 */
	@RequestMapping ("/register")
	@ResponseBody
	public WebJsonBean register(UserEo user){
		boolean result = userSerivce.register(user);
		return new WebJsonBean(CODE.SUCCESS, result);
	}


	/**
	 * 修改密码
	 * @return
	 */
	@RequestMapping ("/modifyPwd")
	@ResponseBody
	public WebJsonBean modifyPwd(UserEo user){
//		user.setAccount(AdminInterceptor.getUserLoginContext().getAccount());
		boolean result = userSerivce.modifyPwd(user);
		return new WebJsonBean(CODE.SUCCESS, result);
	}


	/**
	 * 审核
	 * @param account 用户名
	 * @param flag	0=审核通过 3=审核不通过
	 * @return
	 */
	@RequestMapping ("/audit")
	@ResponseBody
	public WebJsonBean audit(String account,Integer flag){
		boolean result = userSerivce.audit(account,flag);
		return new WebJsonBean(CODE.SUCCESS, result);
	}


	/**
	 * 设置等级
	 * @param account 用户名
	 * @param userLevel
	 * @return
	 */
	@RequestMapping ("/setLevel")
	@ResponseBody
	public WebJsonBean setLevel(String account,String userLevel){
		boolean result = userSerivce.setLevel(account,userLevel);
		return new WebJsonBean(CODE.SUCCESS, result);
	}

	/**
	 * 客户管理列表
	 * @return
	 */
	@RequestMapping ("/userList")
	@ResponseBody
	public WebJsonBean userList(String companyName){
		List<MmdUser> result = userSerivce.userList(companyName);
		return new WebJsonBean(CODE.SUCCESS, result);
	}

	/**
	 * 客户管理列表
	 * @return
	 */
	@RequestMapping ("/modifyUserInfo")
	@ResponseBody
	public WebJsonBean modifyUserInfo(MmdUser mmdUser){
		userSerivce.modifyUserInfo(mmdUser);
		return new WebJsonBean(CODE.SUCCESS);
	}

}
